package com.qiguliuxing.dts.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.qiguliuxing.dts.db.bean.DayStatis;
import com.qiguliuxing.dts.db.dao.DtsUserAccountMapper;
import com.qiguliuxing.dts.db.dao.DtsUserMapper;
import com.qiguliuxing.dts.db.dao.ex.StatMapper;
import com.qiguliuxing.dts.db.domain.DtsUser;
import com.qiguliuxing.dts.db.domain.DtsUserAccount;
import com.qiguliuxing.dts.db.domain.UserVo;

@Service
public class DtsUserService {
	
	@Resource
	private DtsUserMapper userMapper;
	
	@Resource
	private DtsUserAccountMapper userAccountMapper;
	
	@Resource
	private StatMapper statMapper;

	public DtsUser findById(Integer userId) {
		return userMapper.selectById(userId);
	}

	public UserVo findUserVoById(Integer userId) {
		DtsUser user = findById(userId);
		UserVo userVo = new UserVo();
		userVo.setNickname(user.getNickname());
		userVo.setAvatar(user.getAvatar());
		return userVo;
	}

	public DtsUser queryByOid(String openId) {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		wrapper.lambda().eq(DtsUser::getWeixinOpenid, openId);
		wrapper.eq("deleted", false);
		return userMapper.selectOne(wrapper);
	}

	public void add(DtsUser user) {
		user.setAddTime(LocalDateTime.now());
		user.setUpdateTime(LocalDateTime.now());
		userMapper.insert(user);
	}

	public int updateById(DtsUser user) {
		user.setUpdateTime(LocalDateTime.now());
		return userMapper.updateById(user);
	}

	public List<DtsUser> querySelective(String username, String mobile, Integer page, Integer size, String sort,
			String order) {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(username)) {
			wrapper.like("nickname", username);
		}
		if (StrUtil.isNotEmpty(mobile)) {
			wrapper.like("mobile", mobile);
		}
		wrapper.eq("deleted", false);
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)){
			wrapper.orderByDesc(sort);
		}
		Page<DtsUser> iPage = new Page<>(page, size);
		iPage = userMapper.selectPage(iPage, wrapper);
		return iPage.getRecords();
	}

	public int count() {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted", false);
		return userMapper.selectCount(wrapper).intValue();
	}

	public List<DtsUser> queryByUsername(String username) {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		wrapper.eq("username", username);
		wrapper.eq("deleted", false);
		return userMapper.selectList(wrapper);
	}

	public List<DtsUser> queryByMobile(String mobile) {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		wrapper.eq("mobile", mobile);
		wrapper.eq("deleted", false);
		return userMapper.selectList(wrapper);
	}

	public List<DtsUser> queryByOpenid(String openid) {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		wrapper.lambda().eq(DtsUser::getWeixinOpenid, openid);
		wrapper.eq("deleted", false);
		return userMapper.selectList(wrapper);
	}

	public void deleteById(Integer id) {
		userMapper.deleteById(id);
	}

	/**
	 * 审批代理申请
	 */
	public void approveAgency(Integer userId,Integer settlementRate,String shareUrl) {
		//获取账户数据
		QueryWrapper<DtsUserAccount> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsUserAccount::getUserId, userId);
		DtsUserAccount dbAccount = userAccountMapper.selectOne(queryWrapper);
		if (dbAccount == null) {
			throw new RuntimeException("申请账户不存在");
		}
		dbAccount.setShareUrl(shareUrl);
		if (!StringUtils.isEmpty(settlementRate)) {
			dbAccount.setSettlementRate(settlementRate);
		}
		dbAccount.setModifyTime(LocalDateTime.now());
		userAccountMapper.updateById(dbAccount);
		
		//更新会员状态和类型
		DtsUser user = findById(userId);
		user.setUserLevel((byte) 2);//区域代理用户
		user.setStatus((byte) 0);//正常状态
		updateById(user);
		
	}

	public DtsUserAccount detailApproveByUserId(Integer userId) {
		// 获取账户数据
		QueryWrapper<DtsUserAccount> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsUserAccount::getUserId, userId);

		DtsUserAccount dbAccount = userAccountMapper.selectOne(queryWrapper);
		return dbAccount;
	}
	
	public List<DtsUser> queryDtsUserListByNickname(String username,String mobile) {
		QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(username)) {
			wrapper.like("nickname", username);
		}
		if (StrUtil.isNotEmpty(mobile)) {
			wrapper.like("mobile", mobile);
		}
		wrapper.eq("deleted", false);
		return userMapper.selectList(wrapper);
	}

	public List<DayStatis> recentCount(int statisDaysRang) {
		return statMapper.statisIncreaseUserCnt(statisDaysRang);
	}
}
